import { STORAGE_USERINFO_KEY, STORAGE_USERTOKEN_KEY } from "@/consts";
import { ILoginRes, IUser } from "@/types";
import { omit } from "lodash";

/**
 * 存储用户信息、token
 * @param token 登录token
 */
export function storageUserInfo(info: ILoginRes) {
    // 七天后过期
    const expires = Date.now() + (1000 * 60 * 60 * 24 * 7);
    localStorage.setItem(STORAGE_USERTOKEN_KEY, JSON.stringify({
        token: info.token,
        expires
    }));
    localStorage.setItem(STORAGE_USERINFO_KEY, JSON.stringify(omit(info, 'token')));
}

/**
 * 获取用户信息
 * @returns 
 */
export function getUserInfo(): IUser {
    const userStr = localStorage.getItem(STORAGE_USERINFO_KEY);
    try {
        if (userStr) return JSON.parse(userStr);
    } catch (error) {
    }
    return {} as IUser;
}